﻿
namespace Manga.Core
{
    using Models;
    using System;
    using System.Collections.Generic;
    using Windows.Storage.Streams;

    //---------------------------------------------------------------------------------------------------
    //  用于浏览漫画的ComicReader,该Reader支持异步浏览
    //---------------------------------------------------------------------------------------------------
    public class ComicReader
    {
        /// <summary>
        /// 指示当前打开的章节
        /// </summary>
        private readonly IEpisodeModel Episode;

        private readonly IImageProvider Provider;
        /// <summary>
        /// 指示当前服务提供商是否支持异步读取
        /// </summary>
        private readonly bool AsyncViewSupport;
        /// <summary>
        /// 指示当前获取到的页面列表
        /// </summary>
        private readonly List<String> Uri;
        /// <summary>
        /// 获取或设置当前进度
        /// </summary>
        public int Position { get;set; }
        /// <summary>
        /// 指示当前章节的总话数
        /// </summary>
        public int TotalCount { get; private set; }

        public ComicReader(
            IEpisodeModel current,
            IImageProvider provider,bool asyncView)
        {
            Provider = provider;
            Episode = current;
            Position = 1;
            AsyncViewSupport = asyncView;
            Uri = new List<string>();
        }

        async public void Next(bool isEpisode)
        {
            if(isEpisode)
            {
                if (!Provider.CanMoveToNextEpisode)
                {
                    CannotMoveToNextEpisode?.Invoke();
                }
                else
                {
                    if (await Provider.Move(Provider.NextEpisodeUri))
                        OnImageLoaded?.Invoke(Provider.ImageSrc);
                    else
                        OnNetworkError?.Invoke();
                }
            }
            else
            {
                if(!Provider.CanMoveToNext)
                {
                    CannotMoveToNext?.Invoke();
                }
                else
                {
                    if (await Provider.Move(Provider.NextUri))
                        OnImageLoaded?.Invoke(Provider.ImageSrc);
                    else
                        OnNetworkError?.Invoke();
                }
            }
        }

        async public void Prev(bool isEpisode)
        {
            if(isEpisode)
            {
                if (!Provider.CanMoveToPrevEpisode)
                {
                    CannotMoveToPrevEpisode?.Invoke();
                }
                else
                {
                    if (await Provider.Move(Provider.PrevEpisodeUri))
                        OnImageLoaded?.Invoke(Provider.ImageSrc);
                    else
                        OnNetworkError?.Invoke();
                }
            }
            else
            {
                if (!Provider.CanMoveToPrev)
                {
                    CannotMoveToPrev?.Invoke();
                }
                else
                {
                    if (await Provider.Move(Provider.PrevUri))
                        OnImageLoaded?.Invoke(Provider.ImageSrc);
                    else
                        OnNetworkError?.Invoke();
                }
            }
        }

        async public void Start()
        {
            if (await Provider.Move(Episode.Uri))
                OnImageLoaded?.Invoke(Provider.ImageSrc);
            else
                OnNetworkError?.Invoke();
        }

        public event Action<IRandomAccessStream> OnImageLoaded;
        public event Action CannotMoveToNext;
        public event Action CannotMoveToPrev;
        public event Action CannotMoveToNextEpisode;
        public event Action CannotMoveToPrevEpisode;
        public event Action OnNetworkError;
    }
}
